<script src="<?php print $url?>js/jquery.ui.datepicker.js"></script>
<script src="<?php print $url?>js/jquery.ui.autocomplete.js"></script>
<script src="<?php print $url?>js/jquery-ui-timepicker-addon.js"></script>
<style>
  .kecil a{
    color: #CCC;
  }
  .normal a{
    color: gray;
  }
  .libur a{
    color: white;
  }
  .kecil{
    color: #CCC;
  }
  .normal{
    color: gray;
  }
  .libur{
    color: white;
  }
  .libur{
    background-color: #eb4cc2; 
  }
  .inuse{
    background-color: #a3cec4; 
  }
</style>
<div id="main-content">
  <div class="container-fluid">
    <ul class="breadcrumb">
      <li><a href="<?php print   site_url("mrp/asset/schedule/$id_mrp_inventory_department/".($cur_year - 1))?>"><<< Prev</a></li>
			<li class="active"><?php print $cur_year?></li>
			<li><a href="<?php print   site_url("mrp/asset/schedule/$id_mrp_inventory_department/".($cur_year + 1))?>">Next >>></a></li>
			<li><span class="divider">»</span></li>
			<li><a href="<?php print   site_url("mrp/asset/schedule/$id_mrp_inventory_department/".date("Y"))?>">Current Year</a></li>
		</ul>
    <div class="row-fluid">
      <?php
      for($bln = 1 ; $bln <= 3 ; $bln++){
      ?>
      <div class="span4">
        <div class="nonboxy-widget">
					<div class="widget-head">
            <h5><i class="black-icons blocks_images"></i><?php print date("F Y", strtotime("{$cur_year}-{$bln}-01"))?></h5>
					</div>
        </div>
        <div class="widget-content">
          <table class="table table-bordered">
            <thead style='background-color: gray; color: white'>
              <tr>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tue</th>
                <th>Wed</th>
                <th>Thu</th>
                <th>Fri</th>
                <th>Sat</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $aday = strtotime($cur_year."-{$bln}-02") - strtotime("{$cur_year}-{$bln}-01");
              $sel = date("w", (strtotime("{$cur_year}-{$bln}-01")));
              $first_date = date("Y-m-d", (strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel)));
              $int_first_date = strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel);
              
              $jumlah_perbulan = date("t", strtotime("{$cur_year}-{$bln}-01"));
              
              $form_modify = "SELECT 'tanggal' UNION ALL ";
              for($hr = 1 ; $hr <= $jumlah_perbulan ; $hr++){
                $hasil_hari[$hr] = date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"));
                $form_modify .= "SELECT '".date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"))."' ";
                if($hr != $jumlah_perbulan)
                  $form_modify .= "UNION ALL ";
              }
              $query_hari = $this->global_models->get_query("
                SELECT A.tanggal AS tgl, B.*
                FROM ($form_modify) AS A
                LEFT JOIN mrp_history_inventory AS B ON A.tanggal = B.tanggal AND B.id_mrp_inventory_department = $id_mrp_inventory_department
                GROUP BY A.tanggal
                ");
              unset($query_hari[$jumlah_perbulan]);
              $comp = 0;
              for($c = 0 ; $c < 6 ; $c++){
              ?>
              <tr>
                <?php
                for($r = 0 ; $r < 7 ; $r++){
                  $style = "";
                  $kelas = "normal";
                  $minggu = 0;
                  $id_set = "id='single_".date("Y-m-d", $int_first_date)."'";
                  if(date("m", $int_first_date) != $bln){
                    $kelas = 'kecil';
                    $id_set = "";
                  }
                  else{
                    if($query_hari[$comp]->id_mrp_history_inventory){
                      $this->session->set_userdata(
                              array("mrp_block_hari" => date("Y-m-d",(strtotime($query_hari[$comp]->finish))),
                                    "mrp_start_block" => date("Y-m-d",(strtotime($query_hari[$comp]->tanggal))))
                                      );
                    }
                    $comp++;
                  }
                  
                  if($r == 0 OR $r == 6){
                    $kelas .= ' libur';
                    $minggu = 1;
                  }
                  if(date("Y-m-d", $int_first_date) <= $this->session->userdata("mrp_block_hari") AND date("Y-m-d", $int_first_date) >= $this->session->userdata("mrp_start_block")){
                    $kelas = "inuse";
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="fd('{$taggal_harian}')" data-toggle="modal" href="#myModal2">{$hari_harian}</a>
EOD;
                  }
                  else{
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="df('{$taggal_harian}')" data-toggle="modal" href="#myModal">{$hari_harian}</a>
EOD;
                  }
//                  if($this->global_models->get_field("d_hrd_libur", "status", array("tanggal" => date("Y-m-d", $int_first_date))) == 2)
//                    $kelas .= ' libur';
                ?>
                <td <?php print $id_set?> class="<?php print $kelas?>">
                  <?php print $link_harian?>
                </td>
                <?php
                  $int_first_date += $aday;
                }
                ?>
              </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php
      }
      ?>
    </div>
    <div class="row-fluid">
      <?php
      for($bln = 4 ; $bln <= 6 ; $bln++){
      ?>
      <div class="span4">
        <div class="nonboxy-widget">
					<div class="widget-head">
            <h5><i class="black-icons blocks_images"></i><?php print date("F Y", strtotime("{$cur_year}-{$bln}-01"))?></h5>
					</div>
        </div>
        <div class="widget-content">
          <table class="table table-bordered">
            <thead style='background-color: gray; color: white'>
              <tr>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tue</th>
                <th>Wed</th>
                <th>Thu</th>
                <th>Fri</th>
                <th>Sat</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $aday = strtotime($cur_year."-{$bln}-02") - strtotime("{$cur_year}-{$bln}-01");
              $sel = date("w", (strtotime("{$cur_year}-{$bln}-01")));
              $first_date = date("Y-m-d", (strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel)));
              $int_first_date = strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel);
              
              $jumlah_perbulan = date("t", strtotime("{$cur_year}-{$bln}-01"));
              
              $form_modify = "SELECT 'tanggal' UNION ALL ";
              for($hr = 1 ; $hr <= $jumlah_perbulan ; $hr++){
                $hasil_hari[$hr] = date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"));
                $form_modify .= "SELECT '".date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"))."' ";
                if($hr != $jumlah_perbulan)
                  $form_modify .= "UNION ALL ";
              }
              $query_hari = $this->global_models->get_query("
                SELECT A.tanggal AS tgl, B.*
                FROM ($form_modify) AS A
                LEFT JOIN mrp_history_inventory AS B ON A.tanggal = B.tanggal AND B.id_mrp_inventory_department = $id_mrp_inventory_department
                GROUP BY A.tanggal
                ");
              unset($query_hari[$jumlah_perbulan]);
              $comp = 0;
              for($c = 0 ; $c < 6 ; $c++){
              ?>
              <tr>
                <?php
                for($r = 0 ; $r < 7 ; $r++){
                  $style = "";
                  $kelas = "normal";
                  $minggu = 0;
                  $id_set = "id='single_".date("Y-m-d", $int_first_date)."'";
                  if(date("m", $int_first_date) != $bln){
                    $kelas = 'kecil';
                    $id_set = "";
                  }
                  else{
                    if($query_hari[$comp]->id_mrp_history_inventory){
                      $this->session->set_userdata(
                              array("mrp_block_hari" => date("Y-m-d",(strtotime($query_hari[$comp]->finish))),
                                    "mrp_start_block" => date("Y-m-d",(strtotime($query_hari[$comp]->tanggal))))
                                      );
                    }
                    $comp++;
                  }
                  if($r == 0 OR $r == 6){
                    $kelas .= ' libur';
                    $minggu = 1;
                  }
                  if(date("Y-m-d", $int_first_date) <= $this->session->userdata("mrp_block_hari") AND date("Y-m-d", $int_first_date) >= $this->session->userdata("mrp_start_block")){
                    $kelas = "inuse";
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="fd('{$taggal_harian}')" data-toggle="modal" href="#myModal2">{$hari_harian}</a>
EOD;
                  }
                  else{
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="df('{$taggal_harian}')" data-toggle="modal" href="#myModal">{$hari_harian}</a>
EOD;
                  }
//                  if($this->global_models->get_field("d_hrd_libur", "status", array("tanggal" => date("Y-m-d", $int_first_date))) == 2)
//                    $kelas .= ' libur';
                ?>
                <td <?php print $id_set?> class="<?php print $kelas?>">
                  <?php print $link_harian?>
                </td>
                <?php
                  $int_first_date += $aday;
                }
                ?>
              </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php
      }
      ?>
    </div>
    <div class="row-fluid">
      <?php
      for($bln = 7 ; $bln <= 9 ; $bln++){
      ?>
      <div class="span4">
        <div class="nonboxy-widget">
					<div class="widget-head">
            <h5><i class="black-icons blocks_images"></i><?php print date("F Y", strtotime("{$cur_year}-{$bln}-01"))?></h5>
					</div>
        </div>
        <div class="widget-content">
          <table class="table table-bordered">
            <thead style='background-color: gray; color: white'>
              <tr>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tue</th>
                <th>Wed</th>
                <th>Thu</th>
                <th>Fri</th>
                <th>Sat</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $aday = strtotime($cur_year."-{$bln}-02") - strtotime("{$cur_year}-{$bln}-01");
              $sel = date("w", (strtotime("{$cur_year}-{$bln}-01")));
              $first_date = date("Y-m-d", (strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel)));
              $int_first_date = strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel);
              
              $jumlah_perbulan = date("t", strtotime("{$cur_year}-{$bln}-01"));
              
              $form_modify = "SELECT 'tanggal' UNION ALL ";
              for($hr = 1 ; $hr <= $jumlah_perbulan ; $hr++){
                $hasil_hari[$hr] = date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"));
                $form_modify .= "SELECT '".date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"))."' ";
                if($hr != $jumlah_perbulan)
                  $form_modify .= "UNION ALL ";
              }
              $query_hari = $this->global_models->get_query("
                SELECT A.tanggal AS tgl, B.*
                FROM ($form_modify) AS A
                LEFT JOIN mrp_history_inventory AS B ON A.tanggal = B.tanggal AND B.id_mrp_inventory_department = $id_mrp_inventory_department
                GROUP BY A.tanggal
                ");
              unset($query_hari[$jumlah_perbulan]);
              $comp = 0;
              for($c = 0 ; $c < 6 ; $c++){
              ?>
              <tr>
                <?php
                for($r = 0 ; $r < 7 ; $r++){
                  $style = "";
                  $kelas = "normal";
                  $minggu = 0;
                  $id_set = "id='single_".date("Y-m-d", $int_first_date)."'";
                  if(date("m", $int_first_date) != $bln){
                    $kelas = 'kecil';
                    $id_set = "";
                  }
                  else{
                    if($query_hari[$comp]->id_mrp_history_inventory){
                      $this->session->set_userdata(
                              array("mrp_block_hari" => date("Y-m-d",(strtotime($query_hari[$comp]->finish))),
                                    "mrp_start_block" => date("Y-m-d",(strtotime($query_hari[$comp]->tanggal))))
                                      );
                    }
                    $comp++;
                  }
                  if($r == 0 OR $r == 6){
                    $kelas .= ' libur';
                    $minggu = 1;
                  }
                  if(date("Y-m-d", $int_first_date) <= $this->session->userdata("mrp_block_hari") AND date("Y-m-d", $int_first_date) >= $this->session->userdata("mrp_start_block")){
                    $kelas = "inuse";
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="fd('{$taggal_harian}')" data-toggle="modal" href="#myModal2">{$hari_harian}</a>
EOD;
                  }
                  else{
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="df('{$taggal_harian}')" data-toggle="modal" href="#myModal">{$hari_harian}</a>
EOD;
                  }
//                  if($this->global_models->get_field("d_hrd_libur", "status", array("tanggal" => date("Y-m-d", $int_first_date))) == 2)
//                    $kelas .= ' libur';
                ?>
                <td <?php print $id_set?> class="<?php print $kelas?>">
                  <?php print $link_harian?>
                </td>
                <?php
                  $int_first_date += $aday;
                }
                ?>
              </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php
      }
      ?>
    </div>
    <div class="row-fluid">
      <?php
      for($bln = 10 ; $bln <= 12 ; $bln++){
      ?>
      <div class="span4">
        <div class="nonboxy-widget">
					<div class="widget-head">
            <h5><i class="black-icons blocks_images"></i><?php print date("F Y", strtotime("{$cur_year}-{$bln}-01"))?></h5>
					</div>
        </div>
        <div class="widget-content">
          <table class="table table-bordered">
            <thead style='background-color: gray; color: white'>
              <tr>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tue</th>
                <th>Wed</th>
                <th>Thu</th>
                <th>Fri</th>
                <th>Sat</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $aday = strtotime($cur_year."-{$bln}-02") - strtotime("{$cur_year}-{$bln}-01");
              $sel = date("w", (strtotime("{$cur_year}-{$bln}-01")));
              $first_date = date("Y-m-d", (strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel)));
              $int_first_date = strtotime("{$cur_year}-{$bln}-01") - ($aday * $sel);
              
              $jumlah_perbulan = date("t", strtotime("{$cur_year}-{$bln}-01"));
              
              $form_modify = "SELECT 'tanggal' UNION ALL ";
              for($hr = 1 ; $hr <= $jumlah_perbulan ; $hr++){
                $hasil_hari[$hr] = date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"));
                $form_modify .= "SELECT '".date("Y-m-d", strtotime("{$cur_year}-{$bln}-{$hr}"))."' ";
                if($hr != $jumlah_perbulan)
                  $form_modify .= "UNION ALL ";
              }
              $query_hari = $this->global_models->get_query("
                SELECT A.tanggal AS tgl, B.*
                FROM ($form_modify) AS A
                LEFT JOIN mrp_history_inventory AS B ON A.tanggal = B.tanggal AND B.id_mrp_inventory_department = $id_mrp_inventory_department
                GROUP BY A.tanggal
                ");
              unset($query_hari[$jumlah_perbulan]);
//              print "<pre>";
//              print_r($query_hari);
//              print "</pre>";
              
              $comp = 0;
              for($c = 0 ; $c < 6 ; $c++){
              ?>
              <tr>
                <?php
                for($r = 0 ; $r < 7 ; $r++){
                  $style = "";
                  $kelas = "normal";
                  $minggu = 0;
                  $id_set = "id='single_".date("Y-m-d", $int_first_date)."'";
                  if(date("m", $int_first_date) != $bln){
                    $kelas = 'kecil';
                    $id_set = "";
                  }
                  else{
                    if($query_hari[$comp]->id_mrp_history_inventory){
                      $this->session->set_userdata(
                              array("mrp_block_hari" => date("Y-m-d",(strtotime($query_hari[$comp]->finish))),
                                    "mrp_start_block" => date("Y-m-d",(strtotime($query_hari[$comp]->tanggal))))
                                      );
                    }
                    $comp++;
                  }
                  if($r == 0 OR $r == 6){
                    $kelas .= ' libur';
                    $minggu = 1;
                  }
                  if(date("Y-m-d", $int_first_date) <= $this->session->userdata("mrp_block_hari") AND date("Y-m-d", $int_first_date) >= $this->session->userdata("mrp_start_block")){
                    $kelas = "inuse";
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="fd('{$taggal_harian}')" data-toggle="modal" href="#myModal2">{$hari_harian}</a>
EOD;
                  }
                  else{
                    $taggal_harian = date("Y-m-d", $int_first_date);
                    $hari_harian = date("d", $int_first_date);
                    $link_harian = <<<EOD
                        <a onclick="df('{$taggal_harian}')" data-toggle="modal" href="#myModal">{$hari_harian}</a>
EOD;
                  }
//                  if($this->global_models->get_field("d_hrd_libur", "status", array("tanggal" => date("Y-m-d", $int_first_date))) == 2)
//                    $kelas .= ' libur';
                ?>
                <td <?php print $id_set?> class="<?php print $kelas?>">
                  <?php print $link_harian?>
                </td>
                <?php
                  $int_first_date += $aday;
                }
                ?>
              </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
      </div>
      <?php
      }
      ?>
    </div>
  </div>
</div>


<div class="modal hide fade" id="myModal">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Create Schedule</h3>
  </div>
  <div class="modal-body">
    <?php print $this->form_eksternal->form_open("", 'class=" form-horizontal well"')?>
      <fieldset>
        <div class="control-group">
										<label class="control-label">Title</label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input('title', $detail[0]->title, "id='title'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label">Date</label>
										<div class="controls">
                      <?php
                      print $this->form_eksternal->form_date("tanggal", $detail[0]->tanggal, "tanggal")?>
										</div>
									</div>
                  <div class="control-group">
										<label class="control-label">Time</label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input("timein", $detail[0]->timein, "id='timein'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label">Finish</label>
										<div class="controls">
                      <?php
                      print $this->form_eksternal->form_date("finish", $detail[0]->finish, "finish")?>
										</div>
									</div>
                  <div class="control-group">
										<label class="control-label">Time</label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input("timeout", $detail[0]->timeout, "id='timeout'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label">PIC</label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input('pic', $detail[0]->pic, "id='pic'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label">Note</label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_textarea('note', $detail[0]->note, "id='note'")?>
										</div>
									</div>
<!--                  <div class="control-group">
										<label class="control-label">Satus</label>
										<div class="controls">
                      <?php
                      if($detail[0]->status == 1){
                        $tr = TRUE;
                      }
                      else
                        $tr = FALSE;
                      print $this->form_eksternal->form_checkbox('status', 1, $tr); ?>
										</div>
									</div>-->
      </fieldset>
    </form>
    <div class="modal-footer">
      <a href="#" class="btn" data-dismiss="modal">Close</a>
      <a href="#" data-dismiss="modal" class="btn btn-primary">Save changes</a>
    </div>
  </div>
</div>
<div class="modal hide fade" id="myModal2">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Schedule <span id="tanggal_set"></span></h3>
  </div>
  <div class="modal-body">
    <p id="modal_body_p">
    </p>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <a onclick="df($('#tanggal_set').text())" data-dismiss="modal" data-toggle="modal" href="#myModal" class="btn btn-primary">Create New Schedule</a>
  </div>
</div>

<script>
  function df(tanggal){
    $("#tanggal").val(tanggal);
    $("#finish").val(tanggal);
  }
  function fd(tanggal){
    $.post('<?php print   site_url('mrp/asset/kegiatan-inday/'.$id_mrp_inventory_department)?>/'+tanggal,{note:''},function(data){
        $("#tanggal_set").text(tanggal);
        $("#modal_body_p").html(data);
    });
  }
  function save(){
    $.post('<?php print   site_url('mrp/asset/add-history-inventory/'.$id_mrp_inventory_department)?>',{
      title:$('#title').val(),
      tanggal:$('#tanggal').val(),
      timein:$('#timein').val(),
      finish:$('#finish').val(),
      timeout:$('#timeout').val(),
      pic:$('#pic').val(),
      note:$('#note').val()
    },function(data){
        window.location = "<?php print site_url("mrp/asset/schedule/".$id_mrp_inventory_department)?>";
    });
  }
  
  $('#timein').timepicker();
  $('#timeout').timepicker();
</script>